package servlet;

import util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author leiHua
 * @Date 2022/10/14 17:26
 */
@WebServlet("/findMajorByIdServlet.do")
public class FindMajorByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = JDBCUtils.getconnection();
            ps=conn.prepareStatement("select * from tb_major where id=? ");
            ps.setString(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                request.setAttribute("id",rs.getString("id"));
                request.setAttribute("name",rs.getString("major_name"));
            }
            request.getRequestDispatcher("/updateMajor.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                JDBCUtils.close(rs,ps,conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
